package android;

public class QuickSort {
    public static void quickSort(int [] a) {
        int startIndex = 0;
        int endIndex = a.length - 1;
        doSort(startIndex, endIndex,a);
    }

    private static void doSort(int start, int end,int[] a) {
        if (start >= end)
            return;
        int i = start, j = end;
        int cur = i - (i - j) / 2;
        while (i < j) {
            while (i < cur && (a[i] <= a[cur])) {
                i++;
            }
            while (j > cur && (a[cur] <= a[j])) {
                j--;
            }
            if (i < j) {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
                if (i == cur)
                    cur = j;
                else if (j == cur)
                    cur = i;
            }
        }
        doSort(start, cur,a);
        doSort(cur+1, end,a);
    }
}